#!/usr/bin/python
# -*- coding: utf-8 -*-
import omencore as config

def test_read_configuration_file():
    ''' test read configuration file '''
    conf = config.JSONConf.load_from_file('file/test.conf')
    assert conf['key1'] == 'value1'
    assert conf['key2'] == 'value2'
    
def test_write_configuration_file():
    '''test write configuration file'''
    conf = config.JSONConf.load_from_file('file/test.conf')
    conf['key3'] = 'value3'
    config.JSONConf.save_to_file(conf, 'file/test2.conf')
    conf = None
    conf = config.JSONConf.load_from_file('file/test2.conf')
    assert len(conf.keys()) == 3
    assert conf['key3'] == 'value3'
    assert conf['key2'] == 'value2'
    assert conf['key1'] == 'value1'
    
def test_complex_configuration_file():
    '''test complex configuration file'''
    conf = {}
    conf['list'] = ['aaa', 'bbb', 'ccc']
    conf['dico'] = {'key1': 'aaa', 'key2': 'bbb'}
    config.JSONConf.save_to_file(conf, 'file/test3.conf')
    conf = None
    conf = config.JSONConf.load_from_file('file/test3.conf')
    assert 'aaa' in conf['list']
    assert 'bbb' in conf['list']
    assert 'ccc' in conf['list']
    assert conf['dico']['key1'] == 'aaa'
    assert conf['dico']['key2'] == 'bbb'
    
    
def test_checker_ok():
    '''test check key ok'''
    conf = {}
    conf['list'] = ['aaa', 'bbb', 'ccc']
    conf['dico'] = {'key1': 'aaa', 'key2': 'bbb'}
    try:
        config.ConfigurationChecker.check_keys(conf, ['list', 'dico'])
        assert True
    except config.ConfigurationCheckerError:
        assert False
    
def test_checker_ko():
    '''test check key ko'''
    conf = {}
    conf['list'] = ['aaa', 'bbb', 'ccc']
    conf['dico'] = {'key1': 'aaa', 'key2': 'bbb'}
    dico_keys = ['list', 'toto']
    dico_failed = ['toto']
    try:
        config.ConfigurationChecker.check_keys(conf, dico_keys)
        assert False
    except config.ConfigurationCheckerError, err:
        assert str(dico_failed) in str(err)
    
def test_conf_file_with_comment():
    '''test conf file with comment'''
    conf = config.JSONConf.load_from_file('file/test_with_comment.conf')
    assert conf['key1'] == 'value1'
    assert conf['key2'] == 'value2'
    

def test_parse_to_pretty():
    '''test parse_to_pretty_method'''
    conf = config.JSONConf.load_from_file('file/test_with_comment.conf')
    assert conf['key1'] == 'value1'
    assert conf['key2'] == 'value2'
    
    pretty_conf = config.JSONConf.parse_to_pretty(conf)
    assert pretty_conf['key1'] == 'value1'
    assert pretty_conf['key2'] == 'value2'
    assert pretty_conf['aa.bb.dd'] == 'abcd'
    assert pretty_conf['aa.bb.ee'] == 'efgh'
    assert pretty_conf['aa.cc.ff'] == 'ijkl'
    
    